package com.why.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.why.domain.Group;
import com.why.domain.Student;
import com.why.exception.BusinessException;
import com.why.service.GroupService;
import com.why.mapper.GroupMapper;
import com.why.service.StudentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 王海云
 * @description 针对表【group】的数据库操作Service实现
 * @createDate 2024-06-14 19:28:51
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group>
        implements GroupService {
    @Resource
    private StudentService studentService;
    @Override
    @Transactional
    public void addGroup(String groupName) {
        // 参数校验
        if (StringUtils.isBlank(groupName)) {
            throw new BusinessException("组名不能为空！");
        }
        // 组名不能重复
        Group group = lambdaQuery().eq(Group::getGroupName, groupName).one();
        if (group != null) {
            throw new BusinessException("组名已经存在！");
        }
        Group group1 = new Group();
        group1.setGroupName(groupName);
        save(group1);
    }

    @Override
    public void deleteGroup(Long id) {
        // 参数校验
        if (id == null) {
            throw new BusinessException("id不能为空！");
        }
        // 删除组名
        removeById(id);
        // 删除该组内的学生
        studentService.lambdaUpdate().eq(Student::getGroupId, id).remove();
    }
}




